// pages/amount/index.js
import {User} from "../../model/User.js";
const user = new User();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    upload: false,
    loadtype: "loading"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getAmountList({
      type:"first"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this._getAmountList({
      type: "first"
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loadtype != "end") {
      this.setData({
        upload: true,
        loadtype: "loading"
      });
      this._getAmountList({
        type: "more"
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 请求积分记录
   */
  _getAmountList(options){
    if(options.type=="first"){
      this.setData({
        loadtype: "loading"
      });
    }
    user.getAmountList(options)
      .then(res=>{
        let amounts = res.data.Results.map((amount)=>{
          amount.CreateTime = amount.CreateTime.substring(0,10);
        });
        if (options.type == "first") {
          this.setData({
            amounts: res.data.Results
          });
          wx.stopPullDownRefresh();
        } else {
          this.setData({
            amounts: this.data.amounts.concat(res.data.Results),
            upload: false
          })
        }
        if (res.data.TotalPage <= res.data.Page) {
          this.setData({
            upload: true,
            loadtype: "end"
          });
        }
      })
      .catch(err=>{
        wx.showToast({
          title: err.msg,
          icon:"none"
        })
      });
  }
})